package com.xiaogao.serviceOrder.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaogao.commonUtils.JwtUtils;
import com.xiaogao.commonUtils.Result;
import com.xiaogao.serviceOrder.entity.Order;
import com.xiaogao.serviceOrder.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author 小高
 * @since 2021-07-02
 */
@RestController
@RequestMapping("/serviceOrder/order")
//@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;

    // 1、根据课程id和用户id创建订单，返回订单id
    @PostMapping("/createOrder/{courseId}")
    public Result createOrder(@PathVariable("courseId")String courseId, HttpServletRequest request){
        String orderId = orderService.saveOrder(courseId, JwtUtils.getMemberIdByJwtToken(request));

        return Result.ok().data("orderId",orderId);
    }

    // 2、根据订单id查询订单信息
    @GetMapping("/getOrderInfo/{orderId}")
    public Result getOrderInfo(@PathVariable("orderId")String orderId){
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",orderId);
        Order order = orderService.getOne(wrapper);
        return Result.ok().data("item",order);
    }

    // 根据用户id和课程id查询订单信息
    @GetMapping("/isBuyCourse/{memberId}/{courseId}")
    public boolean isBuyCourse(
            @PathVariable String memberId,
            @PathVariable String courseId){
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("member_id",memberId);
        wrapper.eq("course_id",courseId);
        wrapper.eq("status",1);
        // 查询订单状态是 1 则表示 支付成功
        int count = orderService.count(wrapper);
        if (count > 0){
            return true;
        }else{
            return false;
        }
    }

}

